package model

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"rent-ease/config"
)

var DB *gorm.DB

func Init() {
	var err error
	mConfig := config.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mConfig.User,
		mConfig.Password,
		mConfig.Host,
		mConfig.Port,
		mConfig.Database,
	)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	err = DB.AutoMigrate(&User{}, &Rent{})
	if err != nil {
		panic(err)
	}
	fmt.Printf("迁移成功")
}
